<template>
    <el-form ref="ruleFormRef" style="width: 100%" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" :size="formSize" status-icon >
        <el-form-item label="素材名称" prop="title">
            <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="素材名称" prop="url">
            <el-button v-if="!ruleForm.url" type="primary" @click="onUpload">选择文件</el-button>
            <el-image v-else style="width: 100%" :src="ruleForm.url" fit="contain" @click="onUpload" />
        </el-form-item>
    </el-form>
    <el-divider />
    <div class="text-center">
        <el-button type="primary" @click="submitForm(ruleFormRef)" :loading="loading"> 保存 </el-button>
    </div>

    <el-dialog v-model="showUpload" title="图片上传">
        <component :is="Upload" :data="uploadData" @saveUpload="saveUpload" />
    </el-dialog>
</template>
  
<script lang="ts" setup>
import { reactive, ref,watch } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import Upload from '@/components/upload.vue'
import http from '../../../../util/request';
const loading = ref<boolean>(false)

interface RuleForm {
    id: number
    title: string
    url:string
    type:string
}

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<RuleForm>({ id: 0, title: '', url:'',type:'quality'})

let showUpload = ref<any>(false)
let uploadData:any = ref({prefix:'quality/'})

const onGetData = () => {
    http.get('/resource/'+ruleForm.value.id).then((response:any)=>{
        if(response.data.data){
            ruleForm.value = response.data.data
        }
    })
}


const props = defineProps<{
    data:any
}>()

watch(props,()=>{
    if(!props.data){
        ruleForm.value.title = ''
        ruleForm.value.url = ''
    }
    ruleForm.value.id = props.data
    onGetData()
},{ immediate: true })

const rules = reactive<FormRules<RuleForm>>({
    title: [
        { required: true, message: '请输入名称', trigger: 'blur' },
        { min: 2, max: 30, message: '范围在2-30字之间', trigger: 'blur' },
    ],
    url: [
        { required: true, message: '请输入检查内容', trigger: 'blur' },
    ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
    console.log(ruleForm.value)
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            loading.value = true
            let request:any = null
            if(ruleForm.value.id){
                request = http.patch('/resource',ruleForm.value)
            }else{
                request = http.put('/resource',ruleForm.value)
            }
            request.then((response:any)=>{
                loading.value = false
                if(response.data.code != 200){
                    ElMessage.error(response.data.msg);
                    return
                }
                onSave()
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}

const onUpload = () => {
    uploadData.value.img = ruleForm.value.url
    showUpload.value = true
}

const saveUpload = (data:string) => {
    ruleForm.value.url = data
    showUpload.value = false
}

const onSave = () => {
    emit('onSave')
}

const emit = defineEmits(['onSave'])
</script>
<style scoped>
</style>
